package org.origin.centre.utils;

import org.origin.centre.security.user.XUser;
import org.origin.centre.support.utils.BasicUtil;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;

/**
 * 系统用户工具
 *
 * @author ferret
 * @version 2024-05-08
 */
@SuppressWarnings("unused")
public final class UserUtil {

    /**
     * 获取用户信息
     */
    public static UserDetails getUser() {
        SecurityContext context = SecurityContextHolder.getContext();
        if (context != null) {
            Authentication authentication = context.getAuthentication();
            if (authentication != null) {
                Object principal = authentication.getPrincipal();
                if (principal instanceof UserDetails) {
                    return (UserDetails) principal;
                }
            }
        }
        return null;
    }

    /**
     * 获取用户账号
     */
    public static String getAccount() {
        UserDetails user = getUser();
        return user != null ? user.getUsername() : null;
    }

    /**
     * 获取登录的client-id
     *
     * @return client-id
     */
    public static String getClientId() {
        SecurityContext context = SecurityContextHolder.getContext();
        if (context != null) {
            Authentication authentication = context.getAuthentication();
            if (authentication != null) {
                Object principal = authentication.getPrincipal();
                if (principal instanceof User user) {
                    String username = user.getUsername();
                    if (BasicUtil.isNotBlank(username)) {
                        return username;
                    }
                }
            }
        }
        return null;
    }

    /**
     * 获取用户信息
     */
    public static XUser getXUser() {
        UserDetails userDetails = getUser();
        if (userDetails != null) {
            if (userDetails instanceof XUser) {
                return (XUser) userDetails;
            }
        }
        throw new IllegalArgumentException("登录信息获取失败！");
    }

    /**
     * 获取用户ID信息
     */
    public static String getUserId() {
        XUser userDetails = getXUser();
        return userDetails.getId();
    }

    /**
     * 判断是否是超级管理员
     */
    public static boolean isAdmin() {
        XUser userDetails = getXUser();
        return userDetails.isAdmin();
    }

    /**
     * 判断用户是否有权限
     */
    public static boolean hasAnyAuthority(String... authorities) {
        XUser userDetails = getXUser();
        return userDetails.hasAnyAuthority(authorities);
    }

    /**
     * 刷新用户信息及权限
     */
    public static void refresh(UserDetails entity) {
        SecurityContext securityContext = SecurityContextHolder.getContext();
        Authentication authentication = securityContext.getAuthentication();
        // 重新new一个token，因为Authentication中的权限是不可变的.
        UsernamePasswordAuthenticationToken result = new UsernamePasswordAuthenticationToken(
                entity, authentication.getCredentials(), entity.getAuthorities());
        result.setDetails(authentication.getDetails());
        securityContext.setAuthentication(result);
    }

}
